
import {Convert} from '@/utils/custom/Convert'

let TableHelper = {

    //获取列样式及描述
    get_columns(columns) {
        var res = [];
        // 按 Sort 字段升序排序
        columns.sort(function (a, b) {
            return a.Sort - b.Sort;
        });
        columns.forEach((value, index) => {
            var temp = {};
            //动态加载列样式
            temp.title = value.FiledText;//列名称
            temp.dataIndex = value.FiledName;//列索引
            //temp.ellipsis = true,//超过宽度将自动省略
            temp.key = value.Id;//列唯一标识
            temp.width = value.Width;//列宽
            temp.onFilter = function (value, record) { return Convert.convertNullUndefinedToString(Reflect.get(record, temp.dataIndex).toString()).toLowerCase().includes(value.toLowerCase()) };//列过滤函数
            //temp.onFilterDropdownVisibleChange = function (visible) {{ if (visible) { setTimeout(() => { searchInput.focus(); }); } }} ;//列过滤查询菜单
            temp.scopedSlots = { filterDropdown: 'filterDropdown', filterIcon: 'filterIcon', customRender: value.FiledName.toString() };//插槽slot参数定义的名称
            temp.sorter = function (a, b) { return Convert.convertNullUndefinedToString(Reflect.get(a, temp.dataIndex).toString()).length - Convert.convertNullUndefinedToString(Reflect.get(b, temp.dataIndex)).toString().length },//排序函数
                res.push(temp);
        })
        return res;
    },

    get_treeTable(data, parentNodes) {
        let treeData = [];
        let childrenNodes = [];
        parentNodes.forEach(p => {
            childrenNodes = data.filter(d => d.ParentId === p.Id)
            if (childrenNodes.length > 0) {
                childrenNodes = this.get_treeTable(data, childrenNodes)
                p.children = [];
                childrenNodes.forEach(c => {
                    p.children.push(c);
                })

            }
            treeData.push(p);
        })
        return treeData;
    },
}

export default TableHelper